package com.prs.crm.action.base;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.Currency;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeCommon;
import com.prs.crm.service.CurrencyService;
import com.prs.crm.service.sys.CodeCommonService;
import com.prs.crm.service.sys.CodeService;

public class CurrencyAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private CurrencyService currencyService;

	private Currency currency;

	private PaginationSupport pagination;

	private Integer[] currencyIds;

	private CodeCommon codeCommon;
	private CodeCommonService codeCommonService;
	private String Encode;
	private Code code;
	private CodeService codeService;

	private Collection<CodeCommon> codelist;

	public void setCurrencyService(CurrencyService currencyService) {
		this.currencyService = currencyService;
	}

	public CurrencyService getCurrencyService() {
		return currencyService;
	}

	public void setCurrency(Currency currency) {
		this.currency = currency;
	}

	public Currency getCurrency() {
		return currency;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setCurrencyIds(Integer[] currencyIds) {
		this.currencyIds = currencyIds;
	}

	public Integer[] getCurrencyIds() {
		return currencyIds;
	}

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

	@Action("listCurrency")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Currency.class);
		this.setPagination(this.getCurrencyService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("editCurrency")
	public String edit() {
		this.codelist = this.getCodeCommonService().getCodeCommonByCodeType(
				"币种");
		return SUCCESS;
	}

	@Action(value = "saveCurrency", results = {
			@Result(name = "success", type = "redirect", location = "listCurrency"),
			@Result(name = "error", type = "chain", location = "editCurrency") })
	public String save() {
		if (currency.getName() == null || "".equals(currency.getName().trim())) {
			addActionError("名称不能为空");
			return ERROR;
		}
		if (currency.getCurrencycode() == null
				|| "".equals(currency.getCurrencycode().trim())) {
			addActionError("编码不能为空");
			return ERROR;
		}
		
		if (currency.getId() != null) {
		} else {
			if ("n".equals(Encode)) {
				int EncodeRplice = this.getCodeService().getBackRpliceAll(
						"手动编码", "币种", currency.getCurrencycode());
				if (EncodeRplice != 0) {
					this.addActionError("编码重复!请重新输入");
					return ERROR;
				}
			}
			if ("n".equals(Encode)) {
				Code codes = new Code();
				codes.setCodeValue(currency.getCurrencycode());
				codes.setEntityName(this.getClass().getName());
				codes.setCodeType("手动编码");
				codes.setCodeMolde("币种");
				this.getCodeService().save(codes);
				currency.setCode(codes);

			} else {
				CodeCommon Vob = this.getCodeCommonService().getVobject(
						codeCommon.getId());
				Code codes = new Code();
				codes.setEntityName(this.getClass().getName());
				codes.setCodeValue(currency.getCurrencycode());
				Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
				this.codeCommonService.saveOrUpdate(Vob);
				codes.setCodeType(Vob.getCodePlan());
				codes.setCodeMolde("币种");
				this.getCodeService().save(codes);
				currency.setCode(codes);

			}
		}
		this.getCurrencyService().save(currency);
		return SUCCESS;
	}

	@Action(value = "removeCurrency", results = {
			@Result(name = "success", type = "redirect", location = "listCurrency"),
			@Result(name = "error", type = "chain", location = "listCurrency") })
	public String remove() {
		if (currencyIds == null) {
			this.addActionError("请选择要删除的币种");
			return ERROR;
		}
		for (Integer id : currencyIds) {
			Currency currency = this.getCurrencyService().get(id);
			if (currency.getBankAccounts().size() != 0) {
				this.addActionError("已有客户引用，不能删除");
				return ERROR;
			}
		}
		this.getCurrencyService().batchRemove(currencyIds);
		return SUCCESS;
	}

	@Action("viewCurrency")
	public String view() {
		return SUCCESS;
	}

	public CodeCommon getCodeCommon() {
		return codeCommon;
	}

	public void setCodeCommon(CodeCommon codeCommon) {
		this.codeCommon = codeCommon;
	}

	public CodeCommonService getCodeCommonService() {
		return codeCommonService;
	}

	public void setCodeCommonService(CodeCommonService codeCommonService) {
		this.codeCommonService = codeCommonService;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public Collection<CodeCommon> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeCommon> codelist) {
		this.codelist = codelist;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}

}
